<?php
if(!isset($_SESSION)) session_start();
require_once("dbclass.php");

class create {
   private $status=0;  //0 - none, 1 - user NOT created, 2 - user created
   public $db; 
   public $error="&nbsp;";
   private $user_data = array("user"=>"","pass1"=>"","pass2"=>"","email"=>""); 
   public function verifyStr($str) {
       if(preg_match('/^[0-9A-Za-z\._]*$/', $str)) return true;
       else return false;
   }
   
   public function getStatus() {
       return $this->status;
   }
   
   public function connectToDb() {
        $this->db = new dbclass;
        $parameters = array("localhost:3306","Xoomworks","xoom");
        $this->db->connect($parameters)->select_db("eandb");       
   }

   public function getPost() {
       $arrPost = array("user","pass1","pass2","email");
       $arrVerify = array("user");

       if(count($_POST)>0) {
          $this->status = 1; 
          foreach($arrPost as $value) {
              if(!empty($_POST[$value])) {
                  if(in_array($value, $arrVerify) && $this->verifyStr($_POST[$value])) {
                      $this->user_data[$value] = $_POST[$value];
                  } 
                  if(!in_array($value, $arrVerify))
                      $this->user_data[$value] = $_POST[$value];    
              }
          }
       } else $this->status = 0;  
    }
    
    public function getUserData() {
        return $this->user_data;
    }
    
    public function checkIfExists() {
        if($this->user_data["user"]!="") {
            $result = $this->db->query("SELECT * FROM users WHERE username = '".$this->user_data["user"]."'")
                               ->fetch();
        if($result && count($result)>0) return true;
        else return false;
        }
        return false;
    }
    
    public function validateInput() {
        if(count($_POST)==0) {
            $this->status = 0;
            return;
        }
        $this->error = "";
        if($this->user_data["user"]=="") $this->error .= "Please enter a valid username!<br />";
        if($this->user_data["email"]!="" && !filter_var($this->user_data["email"], FILTER_VALIDATE_EMAIL)) $this->error .= "Invalid email address!<br />";
        if($this->user_data["pass1"]=="") $this->error .= "Please enter a password!<br />";
        if($this->user_data["pass1"]!=$this->user_data["pass2"]) $this->error .= "Passwords do not match!<br />";
        if($this->checkIfExists()) $this->error .= "This user already exists!<br />";
        $this->status = 1;
        if($this->error=="") return true;
        return false;
    }
    
    public function insertData() {
        if($this->user_data["user"]!="" && $this->user_data["pass1"]!="") {

            $result = $this->db->query("INSERT INTO users SET username = '".$this->user_data["user"]."',
                                                              password = '".sha1("xw".$this->user_data["pass1"])."',
                                                                 email = '".$this->user_data["email"]."'");  
            $this->status = 2;
       }
    }
    
    public function redirect() {
        header("Location:".$this->user_data["current_url"]);
    }

}

$create = new create;
$create->connectToDb();
$create->getPost();

if($create->validateInput() && $create->getStatus() != 0) {
    $create->insertData();
    $_SESSION["account_created"] = true;
    $_SESSION['login_guard']['postcontents'] = $_POST;
    header("Location:login.php");
}

//$login->redirect();

?>